-- =============================================
-- Create scalar function (FN)
-- =============================================
IF EXISTS (SELECT * 
	   FROM   sysobjects 
	   WHERE  name = N'f_getsorderlastprice')
	DROP FUNCTION f_getsorderlastprice
GO

CREATE FUNCTION f_getsorderlastprice
	(@traderid int, 
	 @bomid int)
RETURNS decimal(18,8)
AS
BEGIN
  declare @sorderlastprice decimal(18,8)
  select top 1 @sorderlastprice=price from tb_saleorder m,tb_saleorderdtl d
    where m.bid=d.bid and m.traderid=@traderid and d.bomid=@bomid
    order by bdate desc
  RETURN @sorderlastprice
END
GO

-- =============================================
-- Example to execute function
-- =============================================


